<?php 
session_start();

$user = $_SESSION['username'];
if (!$user) { header('location: login.php'); exit; }

include "../../includes/functions.php";

echo db_connect();

$days_of_week = array ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$occurrence_id = $_GET['soid'];
$show_id = $_GET['sid']; if (!$show_id) $show_id = $_POST['show_id'];

if ($_POST['submitted']) {
	$s_show_id = $_POST['show_id'];  
	$s_name = $_POST['name'];
  $s_email = $_POST['email'];
  $s_status = $_POST['status'];
  if ($_POST['new']) {
	$query = "INSERT INTO shows (show_id,name,status,email) VALUES ('','$s_name','$s_status','$s_email')";
	if (!mysql_query ($query)) die ("Error saving: ".$query); //echo $query; 
	$show_id = mysql_insert_id();
	} else {
  		$query = "UPDATE shows SET name='$s_name',status='$s_status',email='$s_email' WHERE show_id = '$s_show_id'";   
		if (!mysql_query ($query)) die ("Error saving: ".$query);
	}
  
}


if (!$occurrence_id && !$show_id) $new = 1;

if ($occurrence_id && !$show_id) {
	$query = "SELECT show_id FROM show_occurrances WHERE id = '$occurrence_id'";  
	$result = mysql_query($query);
	$row = mysql_fetch_array ($result);
	$show_id = $row['show_id'];
} 
  	
$sh_query = "SELECT * FROM shows where show_id = '$show_id'";
$sh_result = mysql_query ($sh_query );
$sh_row = mysql_fetch_array($sh_result );
$show_name = $sh_row['name'];
$email = $sh_row['email'];
$status = $sh_row['status'];  
if ($new) {$msg = "New Show";}else{$msg="Editing $show_name";}

// build show list for drow down
$query="SELECT * FROM shows ORDER BY name";
$result= mysql_query($query);

 while ($row= mysql_fetch_array($result)) {
 		$d_show_id=$row['show_id'];
		$d_show_name=$row['name'];
		// put selected if it's today, now logic here
		$select .= "<option value='$d_show_id'>$d_show_name</option>";
 }


 ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CJLY ADMINISTRATION</title>
<link href="../../css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" media="screen" href="../../includes/themes/coffee/grid.css" /> 
<link rel="stylesheet" type="text/css" media="screen" href="../../includes/themes/jqModal.css" /> 

<script type="text/javascript" src="../../calendar_picker_submit.js"></script>
<script type="text/javascript" src="../../includes/jquery-1.3.2.js"></script>
<script type="text/javascript" src="../../includes/js/jqDnR.js"></script>
<script type="text/javascript" src="../../includes/js/jqModal.js"></script>   

<link rel="stylesheet" type="text/css" media="screen" href="themes/ui.datepicker.css" />
<script type="text/javascript" src="../../includes/jquery.jqGrid.js"></script> 
<script type="text/javascript">


// We use a document ready jquery function.
jQuery(document).ready(function(){
	var lastsel2;
	jQuery("#list1").jqGrid({
		url:'_occurrences_grid.php?q=1&show_id=<?=$show_id?>',
		datatype: "xml",
		colNames:['Day of the week','start time', 'end time', 'act'],
		colModel:[
			{name:'day_of_week',index:'day_of_week', width:290, sortable:false, editable:true, edittype:"select", editoptions:{value:{Monday:'Monday',Tuesday:'Tuesday',Wednesday:'Wednesday',Thursday:'Thursday',Friday:'Friday',Saturday:'Saturday',Sunday:'Sunday'}}},
			
			//{name:'type',index:'type', width:60, sortable:false,editable: true,edittype:select, editoptions:{value:{MP3:'MP3',Text:'Text'}}},    
		
			{name:'start_time',index:'start_time', width:60, sortable:false,editable: true,edittype:"text"},
         {name:'end_time',index:'end_time', width:70, sortable:false,editable: true,sorttype:"date"},
			{name:'act',index:'act', width:75,sortable:false}
		],
		rowNum:7,
		rowList:[7,7,7],
		imgpath: 'http://66.118.188.130/*cjly.ca/httpdocs/includes/themes/coffee/images',
		//pager: jQuery('#pager1'),
		sortname: "day_of_week",
		viewrecords: true,
		sortorder: "desc",
		caption:"Schedules for this show",
		editurl:"_save_grid_occurrences.php?show_id=<?=$show_id?>",
		
		
		loadComplete: function(){
		var ids = jQuery("#list1").getDataIDs();
		for(var i=0;i<ids.length;i++){
			var cl = ids[i];
			be = "<input style='height:22px;width:20px;' type='button' value='X' onclick=jQuery('#list1').delGridRow("+cl+"); ></ids>";
			jQuery("#list1").setRowData(ids[i],{act:be})
		}	
	},
		
		
		onSelectRow: function(id){
      if(id && id!==lastsel2){
        jQuery('#list1').restoreRow(lastsel2);
        jQuery('#list1').editRow(id,true);
          lastsel2=id;
      }
    }
  
	}); 

}).navGrid("#list1",{edit:false,add:false,del:false});


jQuery.jgrid.edit = {
    addCaption: "Add a Schedule",
    editCaption: "Edit Schedule",
    bSubmit: "Submit",
    bCancel: "Cancel",
    processData: "Processing...",
	closeAfterAdd:"true",
	
    msg: {
        required:"Field is required",
        number:"Please enter valid number!",
        minValue:"value must be greater than or equal to ",
        maxValue:"value must be less than or equal to"
    }
	
	
};




</script>

</head>

<body bgcolor="#eeeeee">
<a href="index.php"><< ADMINISTRATION Home</a> | <a href="shows.php"><< SHOWS list</a>   
<table width="750" height="60" border="0" cellpadding="0" cellspacing="0">
<form action = "show_edit.php" method = "POST">   
<tr>
    <td colspan="3" class="index_table_hed"><div align="center"><span style="color:black;">SHOWS Manager:</span> <?=$msg?></div></td>
	<td  class="index_table_hed"><div class="edit_controls">  <select name="show_id" id="select">
  <option value="0">-- choose a show --</option>
  <?=$select?>
  </select> &nbsp; &nbsp;<input name="submit" type="submit" value="Edit it">
<br>
<a href="show_edit.php" style="font_size:9; color:white;">>Add a New Show</a>
		</div> 
		</td>
		
</tr> </form>

  </tr>

</table>
<p>


<?php

if ($status == 1) { $checked1 = "selected = 'selected'"; }else{$checked2 = "selected = 'selected'"; }
?>
<br> <br> 
<form name="form1" method="post" action = "show_edit.php">
<div class="edit_form">
Show Name: <input name="name" type="text" value="<?=$show_name;?>">  
<br> <br> Status: <select name='status' autocomplete='off'><option value='1' <?=$checked1?>>Active</option><option value='0' <?=$checked2?>>Inactive</option> </select><br><br> 

Programmer's Email:  <input name="email" type="text" value="<?=$email;?>"> 
</div>
<input name="new" type="hidden" value="<?=$new;?>">
<input name="show_id" type="hidden" value="<?=$show_id?>">
<input name="submitted" type="hidden" value="1">
<br><br> <input type="submit" value="Save Changes" name="submit">
</form>
<br> <br> <br> 

<?php 

if (!$new)  { 
?>


<table id="list1" class="scroll" cellpadding="0" cellspacing="0"></table>
	<input name="Add New Schedule" value="Add New Schedule" type="button" onClick='jQuery("#list1").editGridRow("new", "closeAfterAdd:true, addCaption:Add a new schedule, url:_save_grid_occurrences.php?show_id=<?=$show_id?>");' />

<?php
}
?>

</body>
</html>
